/*******************************************************************************
Provisions Exposure Table
*******************************************************************************/
*Read
u "$cur_sample" if inlist(year,2016,2019), clear
do $cleaning/renameSOI

*Set panel 
gegen t = group(year)
tsset firmid t

*ETR 
g etr = uscetr_soi*100 
g letr = ln(1 + etr)
g detr = d.etr
g dletr = d.letr

*Keep only 2016 + the ETR change 
foreach i in detr dletr {
	gegen x = max(`i'), by(firmid)
	replace `i' = x 
	drop x
}
keep if year==2016

*MTR brackets
g brk0 = taxable_income==0
g brk1 = taxable_income>0 & taxable_income<5e4
g brk2 = taxable_income>=5e4 & taxable_income<7.5e4
g brk3 = taxable_income>=7.5e4 & taxable_income<10e4
g brk4 = taxable_income>=1e5 & taxable_income<3.35e5
g brk5 = taxable_income>=.335e6 & taxable_income<10e6
g brk6 = taxable_income>=10e6 & taxable_income<15e6
g brk7 = taxable_income>=15e6 & taxable_income<18e6
g brk8 = taxable_income>=18e6 & !mi(taxable_income)
g bracket = 0 if brk0 
forval i = 1/8 {
	replace bracket = `i' if brk`i'
} 

*Low vs. High MTR brackets 
g brlo = bracket<=2
g brhi = bracket>=4
g brmi = !brlo & !brhi & !mi(taxable_income)
label var brlo "<=2nd MTR Bracket"
label var brhi ">=4th MTR Bracket"
*AMT 
g amt = alternative_minimum_tax>0 & !mi(alternative_minimum_tax)
label var amt "AMT"

*NOL 
g nol_able = nol_carryforward_available>0 & !mi(nol_carryforward_available)
label var nol_able "NOL"
g nol_used = nol_deduction>0 & !mi(nol_deduction)

*Expensing 
cap drop bonus
g bonus = (inv)>0 & !mi(inv)
label var bonus "Bonus (Inv>0)"

*DPAD 
g dpad = dpad_amt>0 & !mi(dpad_amt)
label var dpad "DPAD"

*Interest limitation
g net_intrst = max(0,interest_paid - interest_received)
g pos_intrst = net_intrst>0
g intlim = net_intrst>.3*(ebitda) & taxable_income>0
label var intlim "Interest Limitation"

*Foreign provisions

label var frn_div "Foreign Repatriations"
label var frn_earn "Foreign Earnings"
label var mne "Multinational"

*Reconstruct a couple variables
g etr_post = etr+detr
g payroll = cost_of_labor + salaries_wages_nde + compensation_officers 
*Winsorize ETR change 
winsor2 detr, cuts(5 95) replace
winsor2 etr, cuts(0 95) replace
winsor2 etr_post, cuts(0 95) replace


/*******************************************************************************
Provisions Exposure Table
*******************************************************************************/

g brk0a = brk0 & net_income>0 & !mi(net_income)
g brk0b = brk0 & !brk0a

global EXPOSURE brk0a brk0b brk1 brk2 brk3 brk4 brk5 brk6 brk7 brk8 amt nol_able nol_used bonus dpad intlim pos_intrst frn_ire frn_earn frn_div frn_gilti

label var brk0 "Fraction of firms with zero taxable income"
label var brk1 "Fraction in 15\% bracket (\<$50K taxable income)"
label var brk2 "Fraction in 25\% bracket (\$50K-\$75K taxable income)"
label var brk3 "Fraction in 34\% bracket (\$75K-\$100K taxable income)"
label var brk4 "Fraction in 39\% bracket (\$100K-\$335K taxable income)"
label var brk5 "Fraction in 34\% bracket (\$335K-\$10M taxable income)"
label var brk6 "Fraction in 35\% bracket (\$10M-\$15M taxable income)"
label var brk7 "Fraction in 38\% bracket (\$15M-\$18M taxable income)"
label var brk8 "Fraction in 35\% bracket (\>$18M taxable income)"

label var amt "Fraction of firms that paid the corporate AMT"

label var nol_able "Fraction of firms that had an NOL carryforward"
label var nol_used "Fraction of firms that used an NOL carryforward"

label var bonus "Fraction of firms with depreciable capital expenditures"

label var dpad "Fraction of firms that used the DPAD"

label var intlim "Fraction of firms above the interest limitation with positive taxable income"
label var pos_intrst "Fraction of firms that had positive net interest expense"

label var frn_ire "Fraction of firms with unrepatriated foreign earnings"
label var frn_earn "Fraction of firms with positive foreign E\&P"
label var frn_div "Fraction of firms receiving dividends from CFCs"
label var frn_gilti "Fraction of firms with foreign E\&P in excess of DTIR"



preserve

gcollapse $EXPOSURE [aw=weightSOI]
format $EXPOSURE  %9.3f
tostring *, force replace u
sxpose, force clear //firstnames 
rename _var1 _var0
g x = _n 
tempfile a1 
save `a1', replace

restore


preserve

gcollapse $EXPOSURE [aw=weightSOI], by(firmtype_alt)
format $EXPOSURE  %9.3f
tostring *, force replace u
sxpose, force clear firstnames 
g x = _n 
merge 1:1 x using `a1', nogen 
drop x
order _var0

g var = "" 
replace var = "Zero taxable income, $>$0 net income" in 1 
replace var = "Zero taxable income, $\leq$0 net income " in 2

replace var = "15\% tax bracket ($<$ 50K taxable income)" in 3
replace var = "25\% bracket (50K-75K taxable income)" in 4
replace var = "34\% bracket (75K-100K taxable income)" in 5
replace var = "39\% bracket (100K-335K taxable income)" in 6
replace var = "34\% bracket (335K-10M taxable income)" in 7
replace var = "35\% bracket (10M-15M taxable income)" in 8
replace var = "38\% bracket (15M-18M taxable income)" in 9 
replace var = "35\% bracket ($>$ 18M taxable income)" in 10

replace var = "Paid the Corporate AMT" in 11

replace var = "Had an NOL carryforward" in 12
replace var = "Used an NOL carryforward" in 13 

replace var = "Depreciable capital expenditures" in 14

replace var = "Claimed the DPAD" in 15 

replace var = "Above interest limit, $>$0 taxable income" in 16
replace var = "Positive net interest expense" in 17 

replace var = "Unrepatriated foreign earnings" in 18 
replace var = "Positive foreign E\&P" in 19 
replace var = "Received dividends from CFCs" in 20 
replace var = "Foreign E\&P in excess of DTIR" in 21

*Format
order var *
set obs 26
g N = _n

replace var = "\textbf{Income Tax Bracket}" in 22 
replace N = 0.5 in 22 

replace var = "\textbf{Exposure to Select Deductions}" in 24
replace N = 10.5 in 23 
replace N = 10.6 in 24 

replace var = "\textbf{Exposure to International Provisions}" in 26 
replace N = 17.5 in 25 
replace N = 17.6 in 26
sort N
drop N

*Export to TeX
listtex * using "$results/exposure_table.tex", replace ///
	rstyle(tabular) ///
	head("\begin{tabular}{l c c c c c c}" /// number of L or R justified columns in the table
	"\toprule" /// thick line at top of the table
	"           & (1)   & (2)           & (3)      & (4)           & (5)  		&  (6) \\" ///
	"    TCJA   & All   & Public        & Public   & Private       & Large Priv  	& Small Priv \\" ///
	" Provision & Firms & Multinational & Domestic & Multinational & Domestic 	& Domestic \\ \midrule") /// 
		foot(   "\bottomrule" /// /// thick line at bottom of the table 
	"\end{tabular}")

restore 
